package com.hctforgreen.greenservice.model;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class MessagesListEntity extends BaseEntity {

	private static final long serialVersionUID = -8787292749589542322L;

	public List<MessagesEntity> dataList = null;

	public String statusCode = "";
	public String msg = "";
	public final static String STATUS_CODE = "statusCode";
	public final static String MSG = "msg";
	private final static String MESSAGE_LIST = "messageList";

	/**
	 * 
	 * @param jsonObject
	 * @param recentMsg是否是最近通知
	 *            1：是
	 * @return
	 * @throws JSONException
	 */
	public static MessagesListEntity parse(JSONObject jsonObject, int recentMsg)
			throws JSONException {
		MessagesListEntity bean = new MessagesListEntity();
		if (jsonObject.has(STATUS_CODE)) {
			bean.statusCode = jsonObject.getString(STATUS_CODE);
		}
		if (jsonObject.has(MSG)) {
			bean.msg = jsonObject.getString(MSG);
		}
		/**
		 * 解析messageList节点
		 */
		if (jsonObject.has(MESSAGE_LIST)) {

			JSONArray jsonArray = jsonObject.getJSONArray(MESSAGE_LIST);

			bean.dataList = new ArrayList<MessagesEntity>();
			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject object = jsonArray.getJSONObject(i);
				MessagesEntity entity = MessagesEntity.parse(object);
				entity.recentMsg = recentMsg;
				bean.dataList.add(entity);
			}

		}

		return bean;
	}

	public static class MessagesEntity extends BaseEntity {

		private static final long serialVersionUID = 4635307216179275243L;

		public String id = "";

		public String msgTitle = "";
		public String seriesName = "";
		public String date = "";
		public int readStatus;// 是否已读 0未读
		public int recentMsg = 0;// 是否是最近通知 1：最近通知 0：正常通知

		public String attachmentPath;// 附件下载地址

		/**
		 * HTML样式
		 */
		public String msgContent = "";

		public String statusCode = "";
		public String msg = "";
		public final static String STATUS_CODE = "statusCode";
		public final static String MSG = "msg";

		public final static String ID = "id";
		public final static String MSG_TITLE = "msgTitle";
		public final static String SERIES_NAME = "seriesName";
		public final static String DATE = "date";
		public final static String MSG_CONTENT = "msgContent";

		public static MessagesEntity parse(JSONObject jsonObject)
				throws JSONException {
			MessagesEntity bean = new MessagesEntity();
			if (jsonObject.has(STATUS_CODE)) {
				bean.statusCode = jsonObject.getString(STATUS_CODE);
			}
			if (jsonObject.has(MSG)) {
				bean.msg = jsonObject.getString(MSG);
			}

			if (jsonObject.has(ID)) {
				bean.id = jsonObject.getString(ID);
			}
			if (jsonObject.has(MSG_TITLE)) {
				bean.msgTitle = jsonObject.getString(MSG_TITLE);
			}
			if (jsonObject.has(SERIES_NAME)) {
				bean.seriesName = jsonObject.getString(SERIES_NAME);
			}
			if (jsonObject.has(DATE)) {
				bean.date = jsonObject.getString(DATE);
			}
			if (jsonObject.has(MSG_CONTENT)) {
				bean.msgContent = jsonObject.getString(MSG_CONTENT);
			}

			if (jsonObject.has("attachmentPath")) {
				bean.attachmentPath = jsonObject.getString("attachmentPath");
			}
			return bean;
		}
	}
}
